package com.seeyon.apps.kk.util;

import java.io.File;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

public class WordToPDF {
	
	private static final int wdFormatPDF = 17; // PDF 格式

    public static boolean wordToPDF(String wordFileName, String pdfFileName) {

        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
            doc = Dispatch.call(docs, "Open", wordFileName).toDispatch();
            File tofile = new File(pdfFileName);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc, "SaveAs", pdfFileName, // FileName
                    wdFormatPDF);
        } catch (Exception e) {
            return false;
        } finally {
            Dispatch.call(doc, "Close", false);
            if (app != null) {
                app.invoke("Quit", new Variant[] {});
            }
        }
        // 如果没有这句话,winword.exe进程将不会关闭
        ComThread.Release();
        return true;
    }
    
    public static void main(String[] args) {
    	String word = "C:/Users/Administrator/Desktop/123.doc";
        String pdf = "C:/Users/Administrator/Desktop/123.pdf";
        boolean isOk = WordToPDF.wordToPDF(word, pdf);
        System.out.println(isOk);
    }
}

